package com.spring.transaction.demotransaction.business;

import com.spring.transaction.demotransaction.entity.User;
import com.spring.transaction.demotransaction.entity.UserExt;
import com.spring.transaction.demotransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserBusinessImpl implements UserBusiness{

    @Autowired
    private UserService userService;

    @Override
    public void saveWithoutTransAction(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequired(userExt,exception2);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void saveWithRequired(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUser(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExt(userExt,exception2);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void saveWithSelfRequired(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequired(userExt,exception2);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void saveWithRequiredCatch(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        try {
            userService.insertUserExtWithRequired(userExt,exception2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void saveWithCatch(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUser(user, exception1);
        userExt.setUserId(user1.getId());
        try {
            userService.insertUserExt(userExt,exception2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void saveWithoutRequiredNew(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequiredNew(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequiredNew(userExt,exception2);
    }

    @Override
    @Transactional
    public void saveWithRequiredNew(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequiredNew(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequiredNew(userExt,exception2);
    }

    @Override
    @Transactional
    public void saveWithOneRequiredNew(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequiredNew(userExt,exception2);
        throw new RuntimeException("模拟异常");
    }

    @Override
    @Transactional
    public void saveWithOneRequiredNewThrow(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithRequiredNew(userExt,exception2);
    }

    @Override
    @Transactional
    public void saveWithOneRequiredNewThrowCatch(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithRequired(user, exception1);
        userExt.setUserId(user1.getId());
        try {
            userService.insertUserExtWithRequiredNew(userExt,exception2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void saveWithWithOutNested(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithNested(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithNested(userExt,exception2);
    }

    @Override
    @Transactional
    public void saveWithWithNestedThrow(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithNested(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithNested(userExt,exception2);
        throw new RuntimeException("模拟异常");
    }

    @Override
    @Transactional
    public void saveWithWithNestedOneThrow(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithNested(user, exception1);
        userExt.setUserId(user1.getId());
        userService.insertUserExtWithNested(userExt,exception2);
    }

    @Override
    @Transactional
    public void saveWithWithNestedOneThrowCatch(User user, boolean exception1, UserExt userExt, boolean exception2) {
        User user1 = userService.insertUserWithNested(user, exception1);
        userExt.setUserId(user1.getId());
        try {
            userService.insertUserExtWithNested(userExt,exception2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
